package com.sinya.shoppingmall.ui.recomend;

import android.content.Context;
import android.support.v4.widget.SlidingPaneLayout;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sinya.shoppingmall.R;
import com.sinya.shoppingmall.bean.BuyNow;

import java.util.ArrayList;
import java.util.List;

/**
 * @author KoizumiSinya
 * @date 2016/1/4.
 */
public class BuyNowListView extends LinearLayout {

    private Context context;

    private SlidingPaneLayout mSlidingPaneLayout;

    private ListView mListView;

    List<BuyNow> beans;

    // [+] 构造函数

    public BuyNowListView(Context context) {
        super(context);
        this.context = context;
    }

    public BuyNowListView(Context context, SlidingPaneLayout mSlidingPaneLayout) {
        this(context);
        this.mSlidingPaneLayout = mSlidingPaneLayout;

        initData();
        initView();
    }

    // [-] 构造函数
    // [+] Init

    private void initData() {

        beans = new ArrayList();
        for(int i = 0; i < 10; i++){
            beans.add(new BuyNow("1", "PRADA/普拉达 女士靴子","仅剩3件","￥3245","￥1823"));
            beans.add(new BuyNow("2", "PRADA/普拉达  新款限量女士牛皮钱包","3","￥3645","￥2223"));
            beans.add(new BuyNow("3", "IPINCO/苹蔻帕 女款裙子","仅剩3件","￥3245","￥1823"));
            beans.add(new BuyNow("1", "IPINCO/苹蔻帕 女款裤子","仅剩3件","￥3245","￥1823"));
            beans.add(new BuyNow("6", "IPINCO/苹蔻帕 女款上衣","仅剩3件","￥3245","￥1823"));
            beans.add(new BuyNow("1", "PRADA/普拉达 女士靴子","仅剩3件","￥3245","￥1823"));
            beans.add(new BuyNow("3", "PRADA/普拉达 女士靴子","仅剩3件","￥3245","￥1823"));
            beans.add(new BuyNow("1", "PRADA/普拉达 女士靴子","仅剩3件","￥3245","￥1823"));
            beans.add(new BuyNow("1", "PRADA/普拉达 女士靴子","仅剩3件","￥3245","￥1823"));
        }
    }

    private void initView() {
        this.addView(View.inflate(context, R.layout.view_buy_now, null));
        mListView = (ListView) findViewById(R.id.listview_buy_now);

        //根据value提取图片资源 动态设置ListView
        setListView();
    }


    private void setListView() {
        BuyNowAdapter adpater = new BuyNowAdapter();
        mListView.setAdapter(adpater);
    }

    // [-] Init
    //[+] class
    public class BuyNowAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (beans != null && beans.size() > 0) {
                return beans.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int position) {
            if (beans != null && beans.size() > 0) {
                return beans.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            BuyNow productBean = beans.get(position);
            Holder holder = null;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.item_buy_now, null);
                holder = new Holder();
                holder.name = (TextView) convertView.findViewById(R.id.name);
                holder.pic = (TextView) convertView.findViewById(R.id.pic);
                holder.price = (TextView) convertView.findViewById(R.id.price);
                holder.limitPrice = (TextView) convertView.findViewById(R.id.limitprice);
                holder.leftTime = (TextView) convertView.findViewById(R.id.leftTime);

                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.name.setText(productBean.name);
            holder.pic.setText(productBean.pic);
            holder.price.setText(productBean.price);
            holder.limitPrice.setText(productBean.limitPrice);
            holder.leftTime.setText(productBean.leftTime);
            return convertView;
        }

    }


    private class Holder {
        public String id;
        public TextView name;
        public TextView pic;
        public TextView price;
        public TextView limitPrice;
        public TextView leftTime;

    }

    //[-]class
    // [+] click

    /**
     * 避免与SlidingPaneLayout滑动重叠
     *
     * @param
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        int act = ev.getAction();
        if (act == MotionEvent.ACTION_DOWN) {
            // 处理侧滑冲突的问题
            if (mSlidingPaneLayout != null) {
                mSlidingPaneLayout.requestDisallowInterceptTouchEvent(true);
            }

        } else if (act == MotionEvent.ACTION_UP) {
            // 处理侧滑冲突的问题
            if (mSlidingPaneLayout != null) {
                mSlidingPaneLayout.requestDisallowInterceptTouchEvent(false);
            }
        }

        return super.onInterceptTouchEvent(ev);
    }

    // [-] click


}
